package weaver.interfaces.solead.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.solead.utils.SoleadUtil;
import weaver.interfaces.solead.wcf.WcfUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-06-06
 * @Description: 采购申请流程结束，调用SAP接口生成采购订单
 * @ModifyLog:
 **/

public class WeaCgddAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        RecordSet rs = new RecordSet();
        writeLog( ">>>>WeaCgddAction=" + requestid );
        JSONObject header = new JSONObject();
        JSONArray bodyList = new JSONArray();
        try {
            rs.executeQuery( "select id,cgxqjhrq,bz,sqr,sqbm,sfxz from " + tablename + " where requestid=?", requestid );
            rs.next();
            String sfxz = Util.null2String( rs.getString( "sfxz" ) );
            if ("0".equals( sfxz )) {//是否有SAP编码：0：是；1：否；
                String mainid = Util.null2String( rs.getString( "id" ) );
                String cgxqjhrq = Util.null2String( rs.getString( "cgxqjhrq" ) );
                String bz = Util.null2String( rs.getString( "bz" ) );
                String sqr = Util.null2String( rs.getString( "sqr" ) );
                String sqbm = Util.null2String( rs.getString( "sqbm" ) );
                ResourceComInfo comInfo = new ResourceComInfo();
                String lastname = comInfo.getLastname( sqr );
                DepartmentComInfo departmentComInfo = new DepartmentComInfo();
                String departname = departmentComInfo.getDepartmentName( sqbm );
                String curdate = DateUtil.getCurrentDate();
                header.put( "DocDate", WcfUtil.getFormatDate( curdate ) );
                header.put( "RequriedDate", WcfUtil.getFormatDate( cgxqjhrq ) );
                header.put( "Comments", bz );
                header.put( "U_FWuser", lastname + "-" + departname );
                header.put( "U_FWprocess", requestid );
                writeLog( ">>>>>header=" + header.toJSONString() );
                String sql = "select wldm,xqsl,cgxqjhrq,xwlgg,bz from "
                        + tablename + "_dt1  where  mainid=? and sfgdzc=0";
                rs.executeQuery( sql, mainid );
                while (rs.next()) {
                    String wldm = Util.null2String( rs.getString( "wldm" ) );
                    String xqsl = Util.null2String( rs.getString( "xqsl" ) );
                    String cgxqjhrq1 = Util.null2String( rs.getString( "cgxqjhrq" ) );
                    String xwlgg = Util.null2String( rs.getString( "xwlgg" ) );
                    String bz1 = Util.null2String( rs.getString( "bz" ) );
                    JSONObject body = new JSONObject();
                    body.put( "ItemCode", wldm );
                    body.put( "Quantity", xqsl );
                    body.put( "RequiredDate", WcfUtil.getFormatDate( cgxqjhrq1 ) );
                    body.put( "U_Spec", xwlgg );
                    body.put( "U_Remark", bz1 );
                    writeLog( ">>>>>body=" + body.toJSONString() );
                    bodyList.add( body );
                }
                writeLog( ">>>>>bodyList=" + bodyList.size() );
                if (bodyList.size() <= 0) {
                    rs.executeUpdate( "update " + tablename + " set sapjkfhzt=2 where requestid=?", requestid );
                }else {
                    String param = SoleadUtil.Cgdd_XMLContent( header, bodyList );
                    Map<String, String> resultMap = WcfUtil.send( param, "solead", "1470000113","" );
                    String code = Util.null2String(resultMap.get( "code" ));
                    String msg = Util.null2String(resultMap.get( "msg" ));
                    String fields = "0".equals( code ) ? "cgddh" : "msg";
                    String isfail = "0".equals( code ) ? "0" : "1";
                    rs.executeUpdate( "update " + tablename + " set sapjkfhzt=" + isfail + "," + fields + "=? where requestid=?", msg, requestid );
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "Action接口异常！" );
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}
